package org.makcro.sms.services.chat.web.client.widgets;

import java.util.ArrayList;
import java.util.Date;

import org.makcro.sms.services.chat.web.client.AppEvents;
import org.makcro.sms.services.chat.web.shared.dto.QuestionDto;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.i18n.client.DateTimeFormat;

public class NewMessagesPanel extends ContentPanel {

	private Button btnAnswerMessage;
	private Button btnRefresh;
	private Grid<QuestionDto> grid;
	private ListStore<QuestionDto> store;
	private DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat( "dd/MM/yyyy 'u' hh:mm" );

	public NewMessagesPanel() {

		setHeading( "Prispele poruke" );
		FitLayout layout = new FitLayout();

		setLayout( layout );
		setScrollMode( Scroll.AUTOY );

		getButtonBar().add( getBtnAnswerMessage() );
		getButtonBar().add( getBtnRefresh() );

		GridCellRenderer<QuestionDto> dateRenderer = new GridCellRenderer<QuestionDto>() {

			@Override
			public Object render(QuestionDto model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<QuestionDto> store, Grid<QuestionDto> grid) {

				Date date = model.get( property );
				return NewMessagesPanel.this.dateTimeFormat.format( date );
			}
		};

		ArrayList<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add( new ColumnConfig( "fromNumber", "Posiljalac", 150 ) );
		ColumnConfig dateColumn = new ColumnConfig( "dateReceived", "Vreme prijema", 150 );
		dateColumn.setRenderer( dateRenderer );
		columns.add( dateColumn );
		columns.add( new ColumnConfig( "question", "Tekst poruke", 180 ) );

		store = new ListStore<QuestionDto>();
		store.setMonitorChanges( true );

		grid = new Grid<QuestionDto>( store, new ColumnModel( columns ) );
		grid.setBorders( false );
		grid.setAutoHeight( false );
		grid.setWidth( 500 );

		/*
		 * grid.getSelectionModel().addSelectionChangedListener(new
		 * SelectionChangedListener<QuestionDto>() {
		 * 
		 * @Override public void
		 * selectionChanged(SelectionChangedEvent<QuestionDto> arg0) {
		 * onQuestionSelected(arg0);
		 * 
		 * } });
		 */

		grid.addListener( Events.OnDoubleClick, new Listener<GridEvent<QuestionDto>>() {

			@Override
			public void handleEvent(GridEvent<QuestionDto> arg0) {
				onGridDoubleClick( arg0 );
			}
		} );

		add( grid );
	}

	protected void onGridDoubleClick(GridEvent<QuestionDto> arg0) {
		// check if any row was selected
		QuestionDto selectedItem = grid.getSelectionModel().getSelectedItem();

		if ( selectedItem != null ) {
			QuestionDto selectedQuestion = arg0.getModel();
			AppEvent gridDoubleClickEvent = new AppEvent( AppEvents.PREPARE_ANSWER, selectedQuestion );
			Dispatcher.get().dispatch( gridDoubleClickEvent );
		}
	}

	protected void onQuestionSelected(SelectionChangedEvent<QuestionDto> arg0) {

	}

	private Button getBtnAnswerMessage() {
		if ( btnAnswerMessage == null ) {
			btnAnswerMessage = new Button( "Odgovori" );
			btnAnswerMessage.addListener( Events.Select, new Listener<ButtonEvent>() {

				@Override
				public void handleEvent(ButtonEvent be) {
					onBtnAnswerSelected();
				}

			} );
		}
		return btnAnswerMessage;
	}

	protected void onBtnAnswerSelected() {
		// check if any question is selected
		QuestionDto selectedItem = grid.getSelectionModel().getSelectedItem();
		if ( selectedItem != null ) {
			AppEvent appEvent = new AppEvent( AppEvents.PREPARE_ANSWER, selectedItem );
			Dispatcher.get().dispatch( appEvent );
		}
		else {
			Info.display( "Obavestenje", "Morate izabrati poruku" );
		}

	}
	
	public Button getBtnRefresh() {
		if ( btnRefresh == null ) {
			btnRefresh = new Button();
			btnRefresh.setText( "Osvezi" );
			btnRefresh.addListener( Events.Select, new Listener<ButtonEvent>() {

				@Override
				public void handleEvent(ButtonEvent be) {
					onRefreshButtonClick(be);
					
				}
			} );
		}
		return btnRefresh;
	}

	protected void onRefreshButtonClick(ButtonEvent be) {
		Dispatcher.get().dispatch( AppEvents.NEW_MESSAGES_UPDATE );
	}

	public Grid<QuestionDto> getGrid() {
		return grid;
	}

	public void setGrid(Grid<QuestionDto> grid) {
		this.grid = grid;
	}

	public ListStore<QuestionDto> getStore() {
		return store;
	}

	public void setStore(ListStore<QuestionDto> store) {
		this.store = store;
	}
}
